package get

import (
	"database/sql"
	_"fmt"

	"github.com/gin-gonic/gin"
)

func All_student(r *gin.Engine) {
	var db *sql.DB
	var err error
	var sno, name, password, level, gender, age, dept, class string = "0", "0", "0", "0", "0", "0", "0", "0"

	db, err = sql.Open("postgres", "port=5432 user=postgres password=system dbname=databasedesign sslmode=disable")
	if err != nil {
		panic(err)
	}

	// 获取搜索
	r.GET("/allstudent", func(c *gin.Context) {
		xx := c.Query("xx")
		if xx != "" {

			str := "SELECT * FROM student"
			rows, err := db.Query(str)
			if err != nil {
				panic(err)
			}
			type student struct {
				Sno 	string
				Name 	string
				Password string
				Level 	string
				Gender	 string
				Age 	string
				Dept 	string
				Class	 string
			}

			allstudent := make([]*student,0)
			for rows.Next() {

				err = rows.Scan(&sno, &name, &password, &level, &gender, &age, &dept, &class)
				if err != nil {
					panic(err)
				}

				allstudent = append(allstudent,&student{
					Sno: sno,
					Name: name,
					Password: password,
					Level: level,
					Gender: gender,
					Age: age,
					Dept: dept,
					Class: class,
				})

				c.JSON(200, gin.H{
					"allstudent": allstudent,
				})
			}
		}

	})
}
